﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RWSBeeldbank.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace RWSBeeldbank.DataModel
{
    /// <summary>
    /// MediaObject group data model.
    /// </summary>
    public class MediaObjectDataGroup : MediaObjectDataCommon 
    {
        public MediaObjectDataGroup()
            : base(String.Empty, String.Empty, String.Empty)
        {
        }

        public MediaObjectDataGroup(String uniqueId, String title, String themeImagePath, String description, int mediaObjectsCount)
            : base(uniqueId, title, themeImagePath)
        {
            this.UniqueId = uniqueId;
            this.Title = title;
            this.SetGroupImage(themeImagePath);
            this.Description = description;
            this.MediaObjectsCount = mediaObjectsCount;
        }

        private ObservableCollection<MediaObjectDataItem> _items = new ObservableCollection<MediaObjectDataItem>();
        public ObservableCollection<MediaObjectDataItem> Items
        {
            get { return this._items; }
        }

        public IEnumerable<MediaObjectDataItem> TopItems
        {
            // Provides a subset of the full items collection to bind to from a GroupedItemsPage
            // for two reasons: GridView will not virtualize large items collections, and it
            // improves the user experience when browsing through groups with large numbers of
            // items.
            //
            // A maximum of 12 items are displayed because it results in filled grid columns
            // whether there are 1, 2, 3, 4, or 6 rows displayed
            get { return this._items.Take(6); }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return this._description; }
            set { this.SetProperty(ref this._description, value); }
        }

        private ImageSource _groupImage;
        private string _groupImagePath;

        public ImageSource GroupImage
        {
            get
            {
                if (this._groupImage == null && this._groupImagePath != null)
                {
                    this._groupImage = new BitmapImage(new Uri(MediaObjectDataCommon._baseUri, this._groupImagePath));
                }
                return this._groupImage;
            }
            set
            {
                this._groupImagePath = null;
                this.SetProperty(ref this._groupImage, value);
            }
        }

        public int MediaObjectsCount { get; set; }

        public void SetGroupImage(String path)
        {
            this._groupImage = null;
            this._groupImagePath = path;
            this.OnPropertyChanged("GroupImage");
        }
    }
}
